/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pa165.languageschoolpresentation;

import com.pa165.languageschoolpersistence.Exceptions.ServiceFailureException;
import com.pa165.languageschoolservice.DTO.CourseDTO;
import com.pa165.languageschoolservice.DTO.LectureDTO;
import com.pa165.languageschoolservice.service.CourseService;
import com.pa165.languageschoolservice.service.LectureService;
import java.text.SimpleDateFormat;
import java.util.List;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.Before;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.LocalizableError;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;
import net.sourceforge.stripes.validation.ValidationErrors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Aleksei Penzentcev
 */
@UrlBinding("/courses/{$event}/{courseDTO.id}")
public class CourseActionBean implements ActionBean {

    final static Logger log = (Logger) LoggerFactory.getLogger(CourseActionBean.class);
    private ActionBeanContext context;
    @SpringBean
    protected CourseService courseService;
    @SpringBean
    protected LectureService lectureService;
    @ValidateNestedProperties(value = {
        @Validate(on = {"saveAdd", "saveEdit"}, field = "name", required = true)})
    private CourseDTO courseDTO;
    private SimpleDateFormat sdfSource = new SimpleDateFormat(LectureActionBean.FORMATSTRING);

    public CourseService getCourseService() {
        return courseService;
    }

    public void setCourseService(CourseService courseService) {
        this.courseService = courseService;
    }

    public LectureService getLectureService() {
        return lectureService;
    }

    public void setLectureService(LectureService lectureService) {
        this.lectureService = lectureService;
    }

    public CourseDTO getCourseDTO() {
        return courseDTO;
    }

    public void setCourseDTO(CourseDTO courseDTO) {
        this.courseDTO = courseDTO;
    }

    public SimpleDateFormat getSdfSource() {
        return sdfSource;
    }

    public void setSdfSource(SimpleDateFormat sdfSource) {
        this.sdfSource = sdfSource;
    }

    public Resolution add() {
        log.debug("add() courseDTO={}", courseDTO);
        return new ForwardResolution("/course/addCourse.jsp");
    }

    public Resolution saveAdd() {
        log.debug("add() courseDTO={}", courseDTO);
        try {
            courseService.add(courseDTO);
            return new RedirectResolution(this.getClass(), "all");
        } catch (ServiceFailureException e) {
            ValidationErrors errors = new ValidationErrors();
            errors.add("course Name", new LocalizableError("courseName.duplicate"));
            getContext().setValidationErrors(errors);
            return getContext().getSourcePageResolution();
        }catch (IllegalArgumentException e) {
            ValidationErrors errors = new ValidationErrors();
            errors.add("course Name", new LocalizableError("courseName.duplicate"));
            getContext().setValidationErrors(errors);
            return getContext().getSourcePageResolution();
        }

    }

    @DefaultHandler
    public Resolution all() {
        log.debug("all()");
        return new ForwardResolution("/course/course.jsp");
    }

    public Resolution delete() throws ServiceFailureException{
        log.debug("delete({})", courseDTO.getId());
        courseService.remove(courseDTO);
        return new RedirectResolution(this.getClass(), "all");
    }

    @Before(stages = LifecycleStage.BindingAndValidation, on = {"edit", "saveEdit", "showLectures"})
    public void loadLectorFromDatabase() throws ServiceFailureException {
        String ids = context.getRequest().getParameter("courseDTO.id");
        if (ids == null) {
            return;
        }
        courseDTO = courseService.get(Long.parseLong(ids));
    }

    public Resolution edit() {
        log.debug("edit() course={}", courseDTO);
        return new ForwardResolution("/course/editCourse.jsp");
    }

    public Resolution saveEdit() {
        log.debug("saveEdit() courseDTO={}", courseDTO);
        try {
            courseService.modify(courseDTO);
            return new RedirectResolution(this.getClass(), "all");
        } catch (ServiceFailureException e) {
            ValidationErrors errors = new ValidationErrors();
            errors.add("course Name", new LocalizableError("courseName.duplicate"));
            getContext().setValidationErrors(errors);
            return getContext().getSourcePageResolution();
        }
    }

    public List<CourseDTO> getCourses() {
        return courseService.getAll();
    }

    public Resolution showLectures() {
        log.debug("showLectures()");
        return new ForwardResolution("/course/courseLectures.jsp");
    }

    public List<LectureDTO> getLectures() throws ServiceFailureException{
        log.debug("getLectures() courseDTO={}", courseDTO);
        return lectureService.getLectures(courseDTO);
    }

    @Override
    public void setContext(ActionBeanContext context) {
        this.context = context;
    }

    @Override
    public ActionBeanContext getContext() {
        return context;
    }
}